/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class TestMySql {

    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement pstmt = null;
        PreparedStatement pstmt2 = null;
//        Statement st = null;
        ResultSet rs = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            conn = Main.getMySqlConnection();
//            st = conn.createStatement();
            String sql = "select * from survey";
//            rs = st.executeQuery(sql);
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
//            pstmt.executeUpdate(sql);
            pstmt2 = conn.prepareStatement("update survey set name=? where id=?");
            int count = 0;
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println("id=" + id + "  name=" + name);

                pstmt2.setString(1, name+"b");
                pstmt2.setInt(2, id);
                int i = pstmt2.executeUpdate();
                count += i;
//                pstmt.executeUpdate("update survey set name=? where id=?");
            }
            System.out.println("count = "+count);

        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(TestJDBC.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                pstmt.close();
                pstmt2.close();
//                st.close();
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
                Logger.getLogger(TestJDBC.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
